package com.example.demo.controller;

import com.example.demo.entity.Order;
import com.example.demo.exception.BizException;
import com.example.demo.service.OrderService;
import com.example.demo.util.EmptyUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController {
    private static final Logger log = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    /**
     * 查询用户列表
     * @return
     */
    @ApiOperation(value = "查询订单列表", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户编号",required = false, paramType = "query"),
            @ApiImplicitParam(name="orderId",value = "订单编号",required = false, paramType = "query")
    })
    @RequestMapping(value = "getOrderList",method = RequestMethod.GET)
    public List<Order> getOrderList(){
        //日志级别总共有TARCE < DEBUG < INFO < WARN < ERROR < FATAL
        log.trace("日志级别--TRACE");
        log.debug("日志级别--DEBUG");
        log.info("日志级别--INFO");
        log.warn("日志级别--WARN");
        log.error("日志级别--ERROR");
        //超时验证
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return orderService.getOrderList();
    }

    /**
     * 新增用户信息
     * @param Order
     * @return
     */
    @ApiOperation(value = "新增用户", notes = "数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户编号",required = true, paramType = "query"),
            @ApiImplicitParam(name="orderId",value = "订单编号",required = true, paramType = "query"),
            @ApiImplicitParam(name="remark",value = "备注",required = true, paramType = "query")
    })
    @RequestMapping(value = "addOrder",method = RequestMethod.GET)
    public List<Order> addOrder(Order Order){
        if(EmptyUtil.isEmpty(Order)){
            throw new BizException();
        }
        try {
            orderService.addOrder(Order);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return orderService.getOrderList();
    }

    /**
     * 修改用户信息
     * @param Order
     * @return
     */
    @RequestMapping(value = "updateOrder",method = RequestMethod.GET)
    public List<Order> updateOrder(Order Order){
        if(null == Order || null == Order.getId()){
            return null;
        }
        try {
            Order queryOrder = new Order();
            queryOrder = orderService.queryOrder(Order);
            if(queryOrder != null ) {
                orderService.updateOrder(Order);
            }else{
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return orderService.getOrderList();
    }
}
